<?php
class Calendar extends Module {
    protected $name = 'Calendar';
    protected $administrable = true;
    protected $version = '2010-08-06';
    protected $details = array('dependencies' => array(),
            'observes' => array('Calendar'));

    const CALENDAR = 'calendar';

    protected $calendarTable = array('id' => 'iPN+', 'date' => 'iN', 'title' =>
            'sN', 'description' => 'T', 'photo' => 'i', 'created' => 'iN');

    protected $calendarRecord = array('table' => self::CALENDAR, 'data' =>
            array());

    protected $filesRoot = 4;

    public function __construct(DBAPI $dbapi, RecordList $recordList) {
        parent::__construct($dbapi, $recordList);
        $this->calendarRecord['properties'] = $this->calendarTable;;
    }

    public function install() {
        Modules::getModule('Users')->registerRights($this->getName(),
                $this->getRights());
        return $this->dbdriver->createTable(self::CALENDAR,
                $this->calendarTable);
    }

    public function uninstall() {
        return $this->dbdriver->dropTable(self::CALENDAR);
    }

    public function onAdd(&$details, $source) {
        if (!$this->hasRights('view admin')) {
            return;
        }
        $calendar = $this->fillRecord($this->calendarRecord, $details);
        $calendar['data']['date'] = strtotime($details['date']);
        $calendar['data']['created'] = time();
        $photo = Modules::getModule('Files')->uploadFiles($_FILES,
                $this->filesRoot);
        if (isset($photo[0])) {
            $calendar['data']['photo'] = $photo[0]['data']['id'];
        }
        $this->dbapi->save($calendar);
    }

    public function onEdit(&$details, $source) {
        if (!$this->hasRights('view admin')) {
            return;
        }
        $calendar = $this->calendarRecord;
        $calendar['data']['id'] = (int) $details['id'];
        $this->dbapi->refresh($calendar);
        $calendar = $this->fillRecord($calendar, $details);
        $photo = Modules::getModule('Files')->uploadFiles($_FILES,
                $this->filesRoot);
        if (isset($photo[0])) {
            $calendar['data']['photo'] = $photo[0]['data']['id'];
        }
        $calendar['data']['date'] = strtotime($details['date']);
        $this->dbapi->update($calendar);
    }

    public function onDelete(&$details, $source) {
        Kernel::setRedirect($this->getLink());
        if (!$this->hasRights('view admin')) {
            return;
        }
        $this->dbdriver->delete(self::CALENDAR, '`id` = ' . (int) $details);
    }

    public function initDisplayAdminEdit() {
        $this->initDisplayAdminAdd();
    }

    public function initDisplayAdminAdd() {
        $this->js('/utils/tiny_mce/tiny_mce.js', false);
        $this->js('/wysiwyg.js');
    }

    public function displayPublicEvent($event) {
        $event = (int) substr($event, strripos($event, '-') + 1);
        $calendar = $this->calendarRecord;
        $calendar['data']['id'] = $event;
        $this->dbapi->refresh($calendar);
        $params = array('title' => $calendar['data']['title'], 'text' =>
                $calendar['data']['description'], 'photo' =>
                $calendar['data']['photo']);
        $this->template('event', $params);
    }

    public function displayPublicIndex() {
        $today = (int) date("j");
        $firstDayWeek = (int) date("w", strtotime(date("01.m.Y")));
        $firstDayWeek = ($firstDayWeek + 6) % 7;
        $lastDay = (int) date("t");
        $cols = array('id', 'title', 'date');
        $items = &$this->dbdriver->select(self::CALENDAR, $cols, '`date` >= ' .
                strtotime(date("1.m.Y")) . ' AND `date` <= ' .
                strtotime(date("t.m.Y")));
        $events = array();
        foreach ($items as &$item) {
            $events[(int) date("j", $item['date'])] = array(
                'title' => $item['title'],
                'link' => $this->getLink('event/' .
                        Kernel::createUrl($item['title']) . '-' . $item['id'] .
                        '/')
            );
        }
        $params = array('today' => $today, 'firstWeek' => $firstDayWeek,
                'lastDay' => $lastDay, 'events' => &$events);
        $this->template('calendar', $params);
    }

    public function displayAdminEdit($id) {
        $calendar = $this->calendarRecord;
        $calendar['data']['id'] = (int) $id;
        $this->dbapi->refresh($calendar);
        $form = &$this->createEditForm('edit', $calendar);
        unset($form['items'][5]);
        $form['items'][1]['value'] = date("d.m.Y", $form['items'][1]['value']);
        $form['items'][3]['type'] = 'textarea';
        $form['items'][3]['cols'] = 80;
        $form['items'][3]['rows'] = 6;
        $form['items'][4]['type'] = 'file';
        $this->displayForm($form);
    }

    public function displayAdminAdd() {
        $form = $this->createAddForm('add', $this->calendarTable);
        unset($form['items'][4]);
        $form['items'][0]['value'] = date("d.m.Y");
        $form['items'][3]['type'] = 'file';
        $form['items'][2]['type'] = 'textarea';
        $form['items'][2]['cols'] = 80;
        $form['items'][2]['rows'] = 6;
        $this->displayForm($form);
    }

    public function displayAdminIndex() {
        $cols = array('id', 'date', 'title');
        $order = array('!date');
        $items = &$this->dbdriver->select(self::CALENDAR, $cols, '', $order);
        foreach ($items as &$item) {
            $item['edit'] = $this->getLink("edit/{$item['id']}/");
            $item['delete'] = $this->getLink($item['id'], 'delete');
        }
        $params = array('items' => &$items);
        $this->template('list', $params);
    }

    public function displayAdminSubmenu() {
        $menu = array(
            array('label' => 'list', 'href' => ''),
            array('label' => 'add', 'href' => 'add/')
        );
        $this->prepareMenu($menu);
        $params = array('menu' => &$menu);
        $this->template('submenu', $params);
    }
}
?>
